#if defined _egm_nick_registration_included
	#endinput
#endif

#define _egm_nick_registration_included 1

	
#include "include/egm/egm_globals.inl"

	

#define NICK_REGISTER_FLAG ADMIN_BAN
#define LEN_PASS 32

new const constVAULT_FILE[] = "EGM_NICK_REGISTER";
new const constSETINFO_FIELD[] = "egm_pass";

/*
*****************************************************************
*								*
*		Nick Register CVAR's				*
*								*
*****************************************************************
egm_nr	(1)
	1/0 -	Enables/disables nick registration

egm_nr_admin_only	(1)
	1/0 -	If 1 only admins can register nicks, else all players

egm_nr_time_for_pass 	(10)
	-	Time for entering password. If 0 than user have to have password set with set_info.

*****************************************************************
*								*
*		Nick Register Commands				*
*								*
*****************************************************************
amx_egm_nr_menu 
	- 	Shows menu for nick registration

	
*/

enum CVARS_NICK_REGISTER{
	CNR_ACTIVE = 0,
	CNR_ADMIN_ONLY,
	CNR_TIME_FOR_PASS
}
enum _:MENU_ITEMS_NICK_REGISTER{
	MINR_REG_PLAYER,
	MINR_REG_NICK,
	MINR_UNREG_PLAYER,
	MINR_UNREG_NICK,
	MINR_SHOW
}
enum PLAYER_INFO_NICK_REGISTER{
	PINR_NAME[LEN_NAME],
	PINR_ID
}
new gCvarsNR[CVARS_NICK_REGISTER];

new gPlayerInfoNR[33][PLAYER_INFO_NICK_REGISTER];


public plugin_init_nick_register() {
	gCvarsNR[CNR_ACTIVE] = register_cvar("egm_nr", "1");
	gCvarsNR[CNR_ADMIN_ONLY] = register_cvar("egm_nr_admin_only", "1");
	gCvarsNR[CNR_TIME_FOR_PASS] = register_cvar("egm_nr_time_for_pass", "10");
	register_concmd("amx_egm_nr_menu", "adminNickRegisterMenu", NICK_REGISTER_FLAG, " - Shows menu for nick registration.");
	register_concmd("amx_egm_nr_register_nick", "registerNick", NICK_REGISTER_FLAG, " <nick> [password] - Registeres nick. If password is not specified, messagemode will be displayed.");
	register_concmd("amx_egm_nr_register_nick_pass", "registerPass", NICK_REGISTER_FLAG, " <password> - Sets password for registered nick.");
	register_concmd("amx_egm_nr_unregister_nick", "unregisterNick", NICK_REGISTER_FLAG, " <nick> - Unegisteres nick.");
}

stock isActiveNickRegister(){
	return get_pcvar_num(gCvarsNR[CNR_ACTIVE]);
}
public adminNickRegisterMenu(id, level, cid){
	if(!cmd_access(id, level, cid, 0)){
		return PLUGIN_HANDLED;
	}
	if(!isActiveNickRegister()){
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_INACTIVE");
		return PLUGIN_HANDLED;
	} 
	new text[64];
	new info[2];
	
	formatex(text, charsmax(text), "%L", id, "NICK_REG_MENU_TITLE");		
	new menu = menu_create(text, "adminNickRegisterMenuHandler");
	
	formatex(text, charsmax(text), "%L", id, "NICK_REG_ITEM_REGISTER_NICK");
	info[0] = MINR_REG_NICK;
	menu_additem(menu, text, info);
	
	formatex(text, charsmax(text), "%L", id, "NICK_REG_ITEM_REGISTER_PLAYER");
	info[0] = MINR_REG_PLAYER;
	menu_additem(menu, text, info);
	
	formatex(text, charsmax(text), "%L", id, "NICK_REG_ITEM_UNREGISTER_NICK");
	info[0] = MINR_UNREG_NICK;
	menu_additem(menu, text, info);
	
	formatex(text, charsmax(text), "%L", id, "NICK_REG_ITEM_UNREGISTER_PLAYER");
	info[0] = MINR_UNREG_PLAYER;
	menu_additem(menu, text, info);
	
	formatex(text, charsmax(text), "%L", id, "NICK_REG_ITEM_SHOW_REGISTERED_NICKS");
	info[0] = MINR_SHOW;
	menu_additem(menu, text, info);
	
	menu_display(id, menu);
		
	
	return PLUGIN_HANDLED;
}

public adminNickRegisterMenuHandler(id, menu, item){
	if(item != MENU_EXIT) {
		new info[2];
		new acc;
		new call;
		menu_item_getinfo(menu,item, acc, info, 1, _, _, call);
		new itemId = info[0];
		switch(itemId){
			case MINR_REG_NICK:  client_cmd(id, "messagemode amx_egm_nr_register_nick");
			case MINR_REG_PLAYER: registerPlayerMenu(id);
			case MINR_UNREG_NICK:  client_cmd(id, "messagemode amx_egm_nr_unregister_nick");
			case MINR_UNREG_PLAYER: unregisterPlayerMenu(id);
			case MINR_SHOW:{}
		}
	}
	menu_destroy(menu);
	
}

public registerNick(id, level, cid){
	if(!cmd_access(id, level, cid, 1)) {
		return PLUGIN_HANDLED;
	}
	if(!isActiveNickRegister()){
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_INACTIVE");
		return PLUGIN_HANDLED;
	} 
	new nick[LEN_NAME];
	new pass[LEN_PASS];
	
	read_argv(1, nick, LEN_NAME-1);
	remove_quotes(nick);
	if(nick[0]){
		if(read_argc() == 3) {
			read_argv(2, pass, LEN_PASS - 1);
			remove_quotes(pass);
			if(pass[0]){
				doRegisterName(id, nick, pass);
				return PLUGIN_HANDLED;
			}
		}
		copy(gPlayerInfoNR[id][PINR_NAME], LEN_NAME -1, nick);
		client_cmd(id, "messagemode amx_egm_nr_register_nick_pass");
	}
	
	return PLUGIN_HANDLED;
}
public registerPass(id, level, cid){
	if(!cmd_access(id, level, cid, 1)) {
		return PLUGIN_HANDLED;
	}
	if(!gPlayerInfoNR[id][PINR_NAME][0]){
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_INACTIVE");
		return PLUGIN_HANDLED;
	}
	new nick[LEN_NAME];
	new pass[LEN_PASS];
	copy(nick, LEN_NAME -1, gPlayerInfoNR[id][PINR_NAME]);
	
	read_argv(1, pass, LEN_NAME-1);
	remove_quotes(pass);	
	if(nick[0]){
		if(pass[0]){
			gPlayerInfoNR[id][PINR_NAME][0] = 0;
			doRegisterName(id, nick, pass);
			new player = gPlayerInfoNR[id][PINR_ID];
			if(player){
				gPlayerInfoNR[id][PINR_ID] = 0;
				if(is_user_connected(player)){
					client_print(player, print_chat, "%L", (id == player?"NICK_REG_SELF" : "NICK_REG_ADMIN", nick, pass));
					client_print(player, print_chat, "%L", "NICK_REG_INFO", constSETINFO_FIELD);
				}
			}
			return PLUGIN_HANDLED;
		}
	} else {
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_SPECIFY_NICK");
	}
	
	return PLUGIN_HANDLED;
}

stock isNickRegistered(const nick[], pass[LEN_PASS] = ""){
	new v = nvault_open(constVAULT_FILE);
	new data[LEN_PASS];
	new timestamp;
	new res = nvault_lookup(v, nick, data, LEN_PASS -1, timestamp);
	if(res)
		copy(pass,LEN_PASS-1, data);
	nvault_close(v);
	return res;
}


stock doRegisterName(id, const name[], const pass[]){
	if(isNickRegistered(name)){
		client_print(id, print_notify, "%L", "NICK_REG_ALREADY_REGISTERED", name);
	} else {
		new v = nvault_open(constVAULT_FILE);
		nvault_set(v, name, pass);
		nvault_close(v);
		new admin[LEN_NAME];
		new steam[LEN_NAME];
		get_user_name(id, admin, LEN_NAME -1);
		get_user_authid(id, steam, LEN_NAME -1);
		log_to_file(constLOG_FILE, "%s[%s] Registered Nick %s with password %s", admin, steam, name, pass);
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_REGISTERED", name, pass);
	}
}

stock doUnregisterName(id, const name[]){
	if(!isNickRegistered(name)){
		client_print(id, print_notify, "%L", "NICK_REG_NOT_REGISTERED", name);
	} else {
		new v = nvault_open(constVAULT_FILE);
		nvault_remove(v, name);
		nvault_close(v);
		new admin[LEN_NAME];
		new steam[LEN_NAME];
		get_user_name(id, admin, LEN_NAME -1);
		get_user_authid(id, steam, LEN_NAME -1);
		log_to_file(constLOG_FILE, "%s[%s] Unegistered Nick %s", admin, steam, name);
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_UNREGISTERED", name);
	}
}

public registerPlayerMenu(id){
	
	if(!isActiveNickRegister()){
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_INACTIVE");
		return PLUGIN_HANDLED;
	}
	new players[32];
	new num;
	new menu;
	new text[64];
	new nick[LEN_NAME];
	formatex(text, charsmax(text), "%L", id, "NICK_REG_TITLE_REG_PLAYER");
	menu = menu_create(text, "registerPlayerMenuHandler");
	new info[2];
	new player;
	get_players(players, num, "ch");
	for(new i=0;i<num;i++){
		player = players[i];
		info[0] = player;
		get_user_name(player, nick, LEN_NAME -1);
		if(!isNickRegistered(nick))
			menu_additem(menu, nick, info);
	}
	menu_display(id, menu);
	return PLUGIN_HANDLED;
}

public registerPlayerMenuHandler(id, menu, item){
	if(item != MENU_EXIT){
		new info[2];
		new acc, call;
		new name[LEN_NAME];
		menu_item_getinfo(menu,item, acc, info, 1, name, LEN_NAME -1, call);
		copy(gPlayerInfoNR[id][PINR_NAME], LEN_NAME-1, name);
		gPlayerInfoNR[id][PINR_ID] = info[0];
		client_cmd(id, "messagemode amx_egm_nr_register_nick_pass");
	} 
	menu_destroy(menu);
	registerPlayerMenu(id);
}

public unregisterPlayerMenu(id){	
	if(!isActiveNickRegister()){
		client_print(id, print_notify, "%L", LANG_PLAYER, "NICK_REG_INACTIVE");
		return PLUGIN_HANDLED;
	}
	new players[32];
	new num;
	new menu;
	new text[64];
	new nick[LEN_NAME];
	formatex(text, charsmax(text), "%L", id, "NICK_REG_TITLE_UNREG_PLAYER");
	menu = menu_create(text, "unregisterPlayerMenuHandler");
	new info[2];
	new player;
	get_players(players, num, "ch");
	for(new i=0;i<num;i++){
		player = players[i];
		info[0] = player;
		get_user_name(player, nick, LEN_NAME -1);
		if(isNickRegistered(nick))
			menu_additem(menu, nick, info);
	}
	menu_display(id, menu);
	return PLUGIN_HANDLED;
}

public unregisterPlayerMenuHandler(id, menu, item){
	if(item != MENU_EXIT){
		new info[2];
		new acc, call;
		new name[LEN_NAME];
		menu_item_getinfo(menu,item, acc, info, 1, name, LEN_NAME -1, call);
		doUnregisterName(id, name);
	} 
	menu_destroy(menu);
	unregisterPlayerMenu(id);
}

stock checkPlayerCanPlay(id){
	new nick[LEN_NAME];
	new pass[LEN_PASS];
	get_user_name(id, nick, LEN_NAME -1);
	if(isNickRegistered(nick, pass)){
		new userPass[LEN_PASS];
		get_user_info(id, constSETINFO_FIELD, userPass, LEN_PASS -1);
		if(equal(pass, userPass)){
			return 1;
		}
		return 0;
	} 
	return 1;
}


